package com.demo.threadJoin;

/**
 * 通过通知的方式 控制线程终止
 * @author gy
 * @date 2022/4/23
 */
public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        T1 t1 = new T1();
        Thread thread1 = new Thread(t1);
        thread1.setName("老大");
        thread1.start();

        for (int i = 0; i < 100; i++) {
            System.out.println("main线程正在执行" + i);
            if(i == 20){
                try {
                    System.out.println("此时 main线程 让出资源 让 老大先执行");
                    thread1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Thread.sleep(1000);
        }
    }
}

class T1 implements Runnable{

    private  int count;

    private boolean loop = true;

    @Override
    public void run() {
        while (loop){
            try {
                System.out.println(Thread.currentThread().getName() + "开始执行了");
                System.out.println(++count);
                Thread.sleep(50);
                if (count == 50){
                    loop = false;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}